package com.sky.controller.user;

import com.sky.constant.MessageConstant;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/shoppingCart")
@Slf4j
@Api(tags = "C端购物车相关接口")
public class ShoppingCartController {
    private final ShoppingCartService cartService;

    @Autowired
    public ShoppingCartController(ShoppingCartService cartService) {
        this.cartService = cartService;
    }

    /**
     * 用户购物车添加商品接口
     *
     * @param cartDTO 购车车dto
     * @return 提示
     */
    @PostMapping("/add")
    @ApiOperation("用户添加购物车")
    public Result<String> add(@RequestBody ShoppingCartDTO cartDTO) {
        log.info("用户添加购物车,信息为:{}", cartDTO);
        cartService.addShoppingCart(cartDTO);
        return Result.success(MessageConstant.ADD_OK);
    }


    /**
     * 获取当前登录用户的购物车
     *
     * @return 购物车所有商品
     */
    @GetMapping("/list")
    @ApiOperation("查看购物车")
    public Result<List<ShoppingCart>> getShoppingCart() {
        log.info("用户查看购物车");
        return Result.success(cartService.list(), MessageConstant.SELECT_OK);
    }


    /**
     * 删除购物车中一个商品
     *
     * @param dto dto
     * @return 提示
     */
    @PostMapping("/sub")
    @ApiOperation("删除购物车中一个商品")
    public Result<String> delete(@RequestBody ShoppingCartDTO dto) {
        log.info("用户删除购物车中一个商品:{}", dto);
        cartService.deleteOneItem(dto);
        return Result.success(MessageConstant.OPERATE_OK);
    }


    /**
     * 清空购物车
     *
     * @return 清空购物车
     */
    @DeleteMapping("/clean")
    @ApiOperation("清空购物车")
    public Result<String> cleanShoppingCart() {
        log.info("用户清空了购物车");
        cartService.clearCart();
        return Result.success(MessageConstant.OPERATE_OK);
    }
}
